package com.niannian.activity;

import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.BaseActivity;
import com.niannian.R;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.web.nnSyncApi;

/**
 * 创建家庭
 * 
 * @author shenglin
 * 
 */
public class CreateFamilyActivity extends BaseActivity {
	Button btn_create_family, btn_out;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_family);
		initTopView();
		initView();
		setLister();
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.GONE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.GONE);
		top_title2.setVisibility(View.VISIBLE);

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

	}

	private void initView() {
		btn_create_family = (Button) findViewById(R.id.btn_create_family);
		btn_out = (Button) findViewById(R.id.btn_out);
	}

	private void setLister() {
		btn_create_family.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new createFamily().init(CreateFamilyActivity.this, null, true, "创建家庭中...").execute();
			}
		});
		btn_out.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				userInfoManager.id = 0;
				try {
					userInfoManager.clearPref();
				} catch (Exception e) {
					e.printStackTrace();
				}
				openActivity(LoginBeforeActivity.class);
				myfinish();
			}
		});
	}

	protected class createFamily extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			Common.tip(CreateFamilyActivity.this, "创建家庭失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				try {
					JSONObject fJson = paramJSONObject.getJSONObject("data");
					userInfoManager.family = fJson.getInt("id");
					userInfoManager.family_perm = 1;
					update_gUser();
					openActivity(HomeActivity.class);
					myfinish();
					Common.tip(CreateFamilyActivity.this, "创建家庭成功");
					return;
				} catch (Exception e) {

				}
			} else {
				Common.tip(CreateFamilyActivity.this, paramJSONObject.getString("msg"));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.createFamily();
		}
	}

	@Override
	public void onBackPressed() {
		exit();
	}
}
